export class Utils {

    // 计算矩形中心到p的连线与矩形的交叉点
    static connPoint(rect, p) {
        rect.x -= 5;
        rect.y -= 5;
        rect.width += 10;
        rect.height += 10;
        var start = p,
            end = {
                x: rect.x + rect.width / 2,
                y: rect.y + rect.height / 2
            };
        // 计算正切角度
        var tag = (end.y - start.y) / (end.x - start.x);
        tag = isNaN(tag) ? 0 : tag;

        var rectTag = rect.height / rect.width;
        // 计算箭头位置
        var xFlag = start.y < end.y ? -1 : 1,
            yFlag = start.x < end.x ? -1 : 1,
            arrowTop, arrowLeft;
        // 按角度判断箭头位置
        if (Math.abs(tag) > rectTag && xFlag == -1) { // top边
            arrowTop = end.y - rect.height / 2;
            arrowLeft = end.x + xFlag * rect.height / 2 / tag;
        } else if (Math.abs(tag) > rectTag && xFlag == 1) { // bottom边
            arrowTop = end.y + rect.height / 2;
            arrowLeft = end.x + xFlag * rect.height / 2 / tag;
        } else if (Math.abs(tag) < rectTag && yFlag == -1) { // left边
            arrowTop = end.y + yFlag * rect.width / 2 * tag;
            arrowLeft = end.x - rect.width / 2;
        } else if (Math.abs(tag) < rectTag && yFlag == 1) { // right边
            arrowTop = end.y + rect.width / 2 * tag;
            arrowLeft = end.x + rect.width / 2;
        }
        return {
            x: arrowLeft,
            y: arrowTop
        };
    }

}